package org.gwtEss.table;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.gwtEss.table.colAdapter.FilterMenuCheckBoxColAdapter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;

public abstract class FilterMenu extends Image {
	PopupPanel filterPopup;
	OnDemandTable<FilterMenuItem> tab;
	private List<FilterMenuItem>  filterList; 
	public FilterMenu() {
		super("images/headerDropMenu.png");
		initListeners();
	}
	public void setFilterList(List<FilterMenuItem> filterList){
		this.filterList = filterList;
	}
	private List subList(List lst, int start, int end){
		List ret = new ArrayList();
		if (lst.size() >= end)
			for (int i=start; i< end; i++)
				ret.add(lst.get(i));
		return ret;
		
	}
//	private List<FilterMenuItem> getCheckedItems(){
//		List<FilterMenuItem> retLst = new ArrayList<FilterMenuItem>();
//		
//		FilterMenuItem filterItem;
//		for (int i=0; i<filterList.size(); i++)
//			if ((filterItem = filterList.get(i)).isChecked())
//				retLst.add(filterItem);
//		return retLst;
//	}
	private void initListeners() {
		addMouseOverHandler(new MouseOverHandler(){

			public void onMouseOver(MouseOverEvent event) {
				setUrl("images/headerDropMenuHilite.png");//TODO: Doesn't work. 
			}
			
		});
		addMouseOutHandler(new MouseOutHandler(){

			public void onMouseOut(MouseOutEvent event) {
				setUrl("images/headerDropMenu.png");
			}
			
		});
		addClickHandler(new ClickHandler(){

			public void onClick(final ClickEvent event) {
				if (filterPopup == null){
					filterPopup = new PopupPanel(true);
					filterPopup.setAnimationEnabled(true);
					filterPopup.addAutoHidePartner(getElement());
					filterPopup.setPopupPosition(event.getNativeEvent().getClientX(), event.getNativeEvent().getClientY() + 10);
					filterPopup.addCloseHandler(new CloseHandler<PopupPanel>(){

						public void onClose(CloseEvent<PopupPanel> event) {
							
//							onFilterMenuClose(getCheckedItems());
							onFilterMenuClose(filterList);
						}
						
					});
				}
				onFilterMenuSelect(new FetchCompleteCallBack<FilterMenuItem> (){

					public void onFetchComplete(List<FilterMenuItem> dataList) {
						filterList = dataList;
						if (filterList != null && filterList.size() > 0){
							
							if (tab == null)
								initTab();
							tab.render(filterList.size());
							
							filterPopup.show();
							filterPopup.setPopupPosition(event.getNativeEvent().getClientX(), event.getNativeEvent().getClientY() + 10);
//							filterPopup.center();
//							filterPopup.setSize("200px", "200px");
						}

					}
					
				});
			}
			
		});		
	}
	public abstract void onFilterMenuSelect(FetchCompleteCallBack<FilterMenuItem> filterListCallBack);
	public abstract void onFilterMenuClose(List<FilterMenuItem> filterCriterias);
	protected void initTab() {
		Cols<FilterMenuItem> cols = new Cols<FilterMenuItem>();
		cols.add(new FilterMenuCheckBoxColAdapter("Filter")); 
		tab = new FilterMenuTable(cols) {};
//		tab.setHeaderVisible(false);
		filterPopup.setWidget(tab);
	}
	private class FilterMenuTable extends OnDemandTable<FilterMenuItem>{

		
		public FilterMenuTable(Cols<FilterMenuItem> cols) {
			super(cols, /*200,150,*/10);
			setNavigationWidgetVisible(false);
			setHeaderVisible(false);
		}

		@Override
		public void fetchRowData(int startRow, int endRow, ColWidget sortCol,
				Boolean sortColAsc, Map<ColWidget, List<FilterMenuItem>> filters,
				FetchCompleteCallBack<FilterMenuItem> fetchCompleteCallBack) {
//			fetchCompleteCallBack.onFetchComplete(filterList.subList(startRow, endRow));
			fetchCompleteCallBack.onFetchComplete(subList(filterList, startRow, endRow));
			
		}

		@Override
		public void fetchColumnUniqueValues(ColWidget col,
				FetchCompleteCallBack<FilterMenuItem> filterListCallBack) {
			filterListCallBack.onFetchComplete(null);//Actually filter list should not be displayed in first place! 
		}
		@Override
		public void render(int totalRows) {
			setRowsPerPage(totalRows);
			super.render(totalRows);
		}

		@Override
		public boolean isAlternateRowColor() {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public void onAfterCellHilite(FilterMenuItem data, ColWidget colWidget) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAfterCellUnHilite(FilterMenuItem data, ColWidget colWidget) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAfterRowHilite(FilterMenuItem data) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAfterRowUnHilite(FilterMenuItem data) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onBeforeCellHilite(FilterMenuItem data,
				ColWidget colWidget) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean onBeforeCellUnHilite(FilterMenuItem data,
				ColWidget colWidget) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean onBeforeRowHilite(FilterMenuItem data) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public boolean onBeforeRowUnHilite(FilterMenuItem data) {
			// TODO Auto-generated method stub
			return true;
		}

		@Override
		public void onRowCellClick(FilterMenuItem data, ColWidget colWidget) {
			// TODO Auto-generated method stub
			
		}
	}
}
